package com.ccjq.shosestore.web.controller.product;

import com.ccjq.shosestore.product.model.BrandModel;
import com.ccjq.shosestore.product.model.ProductModel;
import com.ccjq.shosestore.product.service.ProductService;
import com.ccjq.shosestore.product.service.impl.ProductServiceImpl;
import com.ccjq.shosestore.product.util.PageBean;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends HttpServlet {

	private static final long serialVersionUID = -8209328448641667872L;
	private static final String FALG_INDEX = "index";
	private static final String FALG_PRO = "product";
	private static final String DETAILED = "detailed";
	private static final String BRAND = "brand";
	//private static final String ADDCARD = "add";
	//private static final String ADDCARDS = "adds";

	public ProductServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String flag = request.getParameter("flag");
		System.out.println("-");
		System.out.println("flag=" + flag);

		if (StringUtils.isBlank(flag)) {
			// 空
			toIndexPage(request, response);
		} else if (FALG_INDEX.equals(flag)) {
			// 去首页
			toIndexPage(request, response);
		} else if (FALG_PRO.equals(flag)) {
			// 去商品页(有分页)
			toProPage(request, response);
		} else if (DETAILED.equals(flag)) {
			// 去详情页
			toDetailedPage(request, response);
		} else if (BRAND.equals(flag)) {
			// 去品牌页(有分页)
			toBandPage(request, response);
		} /*else if (ADDCARD.equals(flag)) {
			// 去购物车(单个商品添加)
			toAddCard(request, response);
		} else if (ADDCARDS.equals(flag)) {
			// 去购物车(详情页 需要添加数量的商品添加)
			toAddCards(request, response);
		}*/
	}

	/**
	 * 不数量的添加购物车 详情页
	 * */
	/*private void toAddCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 用户是否登陆验证
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		if (u != null) {
			//已经登录
			request.getRequestDispatcher("ShoppingCartServlet?flag=insert").forward(request, response);
		} else {
			// 未登录 
			request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request, response);
		}
	}*/

	/**
	 * 带数量的添加购物车 主页/列表页
	 * */
	/*private void toAddCards(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 用户是否登陆验证
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		if (u != null) {
			//已登录
			request.getRequestDispatcher("ShoppingCartServlet?flag=insert").forward(request, response);
		} else {
			// 未登录
			request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request, response);
		}
	}*/

	/**
	 * 显示某一品牌的所有商品列表
	 * */
	private void toBandPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String category_id = request.getParameter("category_id");
		ProductService p = new ProductServiceImpl();

		String currentPage = ""; // 当前页 如果空则默认显示第一页
		String pageMethod = ""; // 用户动作 如果空则显示第currentPage页
		currentPage = request.getParameter("currentPage");
		pageMethod = request.getParameter("pageMethod");

		// 清空可能存在的旧Sesrequest
		request.setAttribute("prolist", null);
		// 获取信息
		System.out.println("pageMethod:" + pageMethod + "  /  currentPage:" + currentPage);
		List<ProductModel> list = p.getBrand(category_id, pageMethod, currentPage);
		List<ProductModel> hot = p.getFashionList();// 获取热卖区List
		// 将信息存入HttpSesrequest对象 通过response发到jsq页面
		request.setAttribute("hot", hot);// 热卖区list
		request.setAttribute("prolist", list);// 某品牌list
		request.setAttribute("totalPages", PageBean.getTotalPages());// 总页数
		request.setAttribute("currentPage", PageBean.getCurrentPage());// 当前页
		request.setAttribute("fen", "brand");// 分页标识
		request.setAttribute("category_id", category_id);// 品牌id
		// 转发
		request.getRequestDispatcher("WEB-INF/jsp/product/products.jsp").forward(request, response);
	}

	/**
	 * 显示某一个商品的详细信息
	 * */
	private void toDetailedPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductService p = new ProductServiceImpl();
		String product_id = request.getParameter("product_id");
		// 获取jsp所需的信息
		ProductModel product = p.getProduct(product_id);// 获取商品信息
		String stock = p.getProductQuantity(product_id) + "";// 获取库存
		List<ProductModel> list = p.getBrowseList();// 获取最近浏览List
		List<ProductModel> hot = p.getFashionList();// 获取热卖区List
		String qu = p.getQu(product_id);// 获取销量
		if (qu == null) {// 当销量为空时 手动赋值
			qu = "0";
		}
		// 将信息存入HttpSesrequest对象 通过response发到jsq页面
		request.setAttribute("hot", hot);
		request.setAttribute("qu", qu);
		request.setAttribute("brow", list);
		request.setAttribute("stock", stock);
		request.setAttribute("product", product);
		// 转发
		request.getRequestDispatcher("WEB-INF/jsp/product/product.jsp").forward(request, response);
	}

	/**
	 * 显示所有商品列表
	 * */
	private void toProPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取分页后的list
		ProductService p = new ProductServiceImpl();
		String currentPage = ""; // 当前页 如果空则默认显示第一页
		String pageMethod = ""; // 用户动作 如果空则显示第currentPage页
		currentPage = request.getParameter("currentPage");
		pageMethod = request.getParameter("pageMethod");
		List<ProductModel> list = p.getProductList(pageMethod, currentPage);
		// 获取热卖
		List<ProductModel> hotlist = p.getFashionList();
		// 将信息存入HttpSesrequest对象 通过response发到jsq页面
		request.setAttribute("hot", hotlist);// 热卖
		request.setAttribute("totalPages", PageBean.getTotalPages());// 总页数
		request.setAttribute("currentPage", PageBean.getCurrentPage());// 当前页
		request.setAttribute("prolist", list);// 全部商品信息
		request.setAttribute("fen", "product");// 分页标识
		// 转发
		request.getRequestDispatcher("WEB-INF/jsp/product/products.jsp").forward(request, response);
	}

	/**
	 * 打开主页时需要加载的数据
	 * */
	private void toIndexPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductService p = new ProductServiceImpl();
		List<ProductModel> newp = p.getNewproductList();// 获取新品区List
		List<ProductModel> hot = p.getFashionList();// 获取热卖区List
		List<ProductModel> scroll = p.getScroll();// 获取滚动List
		List<BrandModel> brand = p.getBrandTextList();//获取品牌列表
		request.setAttribute("scroll", scroll);
		request.getSession().setAttribute("hot", hot);
		request.setAttribute("newp", newp);
		request.getSession().setAttribute("brand", brand);
		request.getRequestDispatcher("WEB-INF/jsp/index.jsp").forward(request, response);
	}
}
